﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using T4UtilityBelt.DataDesigns;

namespace T4UtilityBelt.LightSwitch.FileModels
{
    public partial class ModelFragment
    {
        public static ModelFragment Load(string fileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ModelFragment));

            using (FileStream sr = File.OpenRead(fileName))
            {
                return xs.Deserialize(sr) as ModelFragment;
            }
        }

        public EntityModel ToModel()
        {
            EntityModel model = new EntityModel();

            Dictionary<string, Association> associations = new Dictionary<string, Association>();
            Dictionary<string, EntitySet> entitySets = new Dictionary<string, EntitySet>();
            Dictionary<string, Entity> entities = new Dictionary<string, Entity>();

            foreach (Association association in Association)
            {
                if (!associations.ContainsKey(association.Name))
                {
                    associations.Add(association.Name, association);
                }
            }

            foreach (EntityContainer entityContainer in EntityContainer)
            {
                foreach (EntitySet entitySet in entityContainer.EntitySet)
                {
                    if (!entitySets.ContainsKey(entitySet.Name))
                    {
                        entitySets.Add(entitySet.EntityType, entitySet);
                    }
                }
            }

            foreach (EntityType entityType in this.EntityTypes)
            {
                Entity entity = new Entity();
                entity.Name = entityType.Name;

                if (entitySets.ContainsKey(entityType.Name))
                {
                    EntitySet entitySet = entitySets[entityType.Name];
                    entity.PluralName = entitySet.Name;
                }

                model.Entities.Add(entity);
                entities.Add(entity.Name, entity);
            }

            //todo read the LightSwitch Model and correctly translate this to a TeaLight EntityModel

            //foreach (EntityType entityType in this.EntityTypes)
            //{
            //    Entity entity = entities[entityType.Name];

            //    foreach (EntityTypeProperties properties in entityType.EntityTypeProperties)
            //    {
            //        if (properties.KeyProperty != null)
            //        {
            //            foreach (KeyProperty property in properties.KeyProperty)
            //            {
            //                KeyProperty kp = new KeyProperty {  Name = property.Name, PropertyType = property.PropertyType };
            //                if (property.KeyPropertyAttributes != null
            //                    && property.KeyPropertyAttributes.Length > 0)
            //                {
            //                    KeyPropertyAttributes kpa = property.KeyPropertyAttributes[0];
            //                    kp.PropertyType = property.PropertyType;
            //                    //kp.NotSearchable = kpa.NotSearchable != null;
            //                    //kp.Required = kpa.Required != null;
            //                }
            //                entity.Properties.Add(kp);
            //            }
            //        }

            //        if (properties.EntityProperty != null)
            //        {
            //            foreach (EntityProperty property in properties.EntityProperty)
            //            {
            //                T4UtilityBelt.EntityModel.Property kp = new EntityModel.Property { Entity = entity, Name = property.Name, PropertyType = property.PropertyType };
            //                if (property.EntityPropertyAttributes != null
            //                    && property.EntityPropertyAttributes.Length > 0)
            //                {
            //                    EntityPropertyAttributes kpa = property.EntityPropertyAttributes[0];

            //                    kp.Required = kpa.Required != null;

            //                    if (kpa.MaxLength != null)
            //                    {
            //                        if (kpa.MaxLength.Length >= 1)
            //                        {
            //                            int l;
            //                            if (int.TryParse(kpa.MaxLength[0].Value, out l))
            //                                kp.MaxLength = l;
            //                        }
            //                        if (kpa.MaxLength.Length >= 2)
            //                        {
            //                            int l;
            //                            if (int.TryParse(kpa.MaxLength[1].Value, out l))
            //                                kp.MaxDecimals = l;
            //                        }
            //                    }
            //                }
            //                entity.Properties.Add(kp);
            //            }
            //        }

            //        if (properties.NavigationProperty != null)
            //        {
            //            foreach (NavigationProperty property in properties.NavigationProperty)
            //            {
            //                T4UtilityBelt.EntityModel.NavigationProperty2 kp =
            //                    new EntityModel.NavigationProperty2
            //                        {
            //                            Name = property.Name,
            //                            Association = property.Association,
            //                            FromEnd = property.FromEnd
            //                        };
            //                kp.PropertyType = ":Int32";
            //                kp.FromEndEntity = entities[kp.FromEnd];
            //                kp.ToEnd = property.ToEnd;
            //                kp.ToEndEntity = entities[kp.ToEnd];
            //                if (associations.ContainsKey(property.Association))
            //                {
            //                    kp.Multiplicity = GetMultiplicity(associations[property.Association], entity);
            //                }
            //                entity.NavigationProperties2.Add(kp);
            //            }
            //        }
            //    }
            //}

            return model;
        }

        //private Multiplicity GetMultiplicity(Association association, Entity entity)
        //{
        //    if (association != null)
        //    {
        //        foreach (AssociationEnd associationEnd in association.AssociationEnd)
        //        {
        //            if (!string.IsNullOrEmpty(associationEnd.Multiplicity)
        //                && associationEnd.EntityType == entity.Name)
        //            {
        //                return (Multiplicity)Enum.Parse(typeof(Multiplicity), associationEnd.Multiplicity);
        //            }
        //        }
        //    }
        //    return Multiplicity.None;
        //}
    }
}